package com.luocc.tim.recycler;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.luocc.tim.R;
import com.luocc.tim.TimApplication;
import com.luocc.tim.entity.Message;

import java.util.List;

public class ChatRecordAdapter extends RecyclerView.Adapter<ChatRecordAdapter.RecordHolder> {
    public static final String TAG = "ChatRecordAdapter";
    private final Context context;
    private final List<Message> messages;


    public ChatRecordAdapter(Context context, List<Message> messages) {
        this.context = context;
        this.messages = messages;
    }

    @NonNull
    @Override
    public RecordHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.layout_message, parent, false);
        return new RecordHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull RecordHolder holder, int position) {
        Message message = messages.get(position);
        TextView textView = (TextView) LayoutInflater
                .from(context)
                .inflate(R.layout.textview_message, (ViewGroup) holder.itemView, false);
        textView.setBackground(ContextCompat.getDrawable(context, R.drawable.message_background));
        textView.setText(message.getContent());
        ImageView imageView = new ImageView(context);
        int iid = View.generateViewId();
        imageView.setId(iid);
        RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        if (TimApplication.getSelf().equals(message.getSender())) {
            imageView.setImageResource(R.mipmap.photo_boy);
            layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            layoutParams2.addRule(RelativeLayout.LEFT_OF, iid);
        } else {
            imageView.setImageResource(R.mipmap.photo_girl);
            layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            layoutParams2.addRule(RelativeLayout.RIGHT_OF, iid);
        }
        ((ViewGroup) holder.itemView).removeAllViews();
        ((ViewGroup) holder.itemView).addView(imageView, layoutParams1);
        ((ViewGroup) holder.itemView).addView(textView, layoutParams2);
    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    public void addData(Message message) {
        messages.add(message);
        notifyItemInserted(messages.size());
    }

    public static class RecordHolder extends RecyclerView.ViewHolder {

        public RecordHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
